﻿/*!
* common.js For jQuery v1.0.0
* http://www.spsoft.com
* Copyright 2013, SEVEN
* 
* Email: zdh082@qq.com
* Date: 2013-03-15
*/
$.sp = {
    Alert: function (title, isok) {
        var $alert = $("#alert");
        if ($alert.size() == 0) {
            $alert = $('<div id="alert" style="width:323px;z-index:8081; height:83px;position:absolute;overflow:hidden">\
                                <div style="color:#fff">\
                                    <p style="padding:30px 10px 10px 90px;"></p>\
                                </div>\
                        </div>');
            $alert.appendTo($("body"));
        }
        if (isok) {
            $alert.css({ background: "url(/Content/Default/img/popyes.png) 0 0 no-repeat" });
        } else {
            $alert.css({ background: "url(/Content/Default/img/popno.png) 0 0 no-repeat" });
        }
        $("p", $alert).text(title);
        $alert.css({ left: ($(window).width() - 323) / 2, top: ($(window).height() - 83) / 2 });
        $alert.stop().fadeIn(300).delay(1500).fadeOut(400);
    },

    Loading: function (title) {

        var div = $("#loadingcoverdiv");
        if (div.size() == 0) {
            div = $("<div id='loadingcoverdiv'></div>");
            div.appendTo($("body"));
        }
        div.css({ background: "#000", opacity: 0.8, zIndex: 100, position: "absolute", top: 0 })
                          .width($(document).width())
                          .height($(document).height()).show();

        $("select").attr("hideselect", "1").hide();
        div.empty();
        var content = $("#loadingtitle");
        if (content.size() == 0) {
            content = $("<span id='loadingtitle'></span>");
            content.appendTo($("body"));
        }
        content.css({ position: "fixed",
            display: "inline-block",
            zIndex: 8080,
            background: "#fff url(/Content/Default/Img/loading.gif) 7px 7px no-repeat",
            boxShadow: "0 1px 10px rgba(0, 0, 0, 0.9)",
            height: 46,
            width: 46,
            opacity: 0.8,
            borderRadius: 50,
            color: "#fff",
            left: ($(window).width() - 32) / 2,
            top: ($(window).height() - 32) / 2
        });
    },
    LoadCompleted: function () {
        $("#loadingcoverdiv").remove();
        $("#loadingtitle").remove();
        $("select[hideselect]").show();
    },
    Box: function (title, dheight) {
        var height = 400;
        if (typeof (dheight) != "undefined") height = dheight;
        fusion2.canvas.getClientRect
        ({
            onSuccess: function (rect) {
                $("#mask").css({ width: $(document).width(), height: $(document).height() }).show();
                var $AsyncBox = $("#AsyncBox");
                $AsyncBox.height(height);
                $("h5", $AsyncBox).text(title);
                $AsyncBox.css({ left: ($(window).width() - 682) / 2 + rect.left, top: (rect.bottom - rect.top - height / 2) / 2 + rect.top });
                $AsyncBox.fadeIn(10);
            }
        });
    },

    BoxClose: function () {
        var $AsyncBox = $("#AsyncBox");
        $AsyncBox.fadeOut(300);
        $("#mask").hide();
        $("h5", $AsyncBox).text("");
        $(".content", $AsyncBox).html("");
    },


    PopUp: function (html, model, flag, width, height, fun) {
        var myheight = 500;
        var mywidth = 300;
        var contentbgimg = '';
        if (!!width && width != "") mywidth = width;
        if (!!height && height != "") myheight = height;
        if (!!flag && flag != "") contentbgimg = '/Resources/default/images/doll-' + flag + '.png';

        var div = $("#popupdiv");
        if (div.size() == 0) {
            div = $("<DIV id='popupdiv'></div>");
            div.appendTo($("body"));
        }
        div.css({ background: "#000", opacity: 0.6, zIndex: 9998, position: "absolute", top: 0, left: 0 })
                          .width($(document).width())
                          .height($(document).height()).show();

        $("select").attr("hideselect", "1").hide();

        var content = $("#popupcontent");
        if (content.size() == 0) {
            content = $("<div id='popupcontent'><div class='hd'><h5 class='clearfix'><span></span><a href='javascript:;' class='close'>×</a></h5></div><div class='content'></div><div class='fd'></div></div>");

            //设置内容样式
            content.find('.hd').css({ height: 28, "margin-top": "10px" });
            content.find('h5').css({ "padding-right": "20px" }).find('a.close').css({ display: "block", width: 28, height: 28, textIndent: -9999, "float": "right" }).click(function () {
                $.sp.PopUpClose();
            });
            content.find(".content").css({ width: mywidth - 38, "margin": "0 auto", "text-align": "center" });
            content.find(".content").html(html);
            content.appendTo($("body"));
        }

        if (model) content.find('h5').hide();

        content.css({ position: "absolute",
            zIndex: 9999,
            background: 'url(/Resources/default/images/popTxt2.png) 0 0 no-repeat',
            height: myheight,
            width: mywidth,
            left: ($(window).width() - mywidth) / 2,
            top: ($(window).height() - myheight) / 2 + $(document).scrollTop()
        });
        $("#popupdiv").show();
        $("#popupcontent").show();
        if ($.isFunction(fun)) fun();

    },
    PopUpClose: function () {
        $("#popupdiv").hide();
        $("#popupcontent").hide();
        $("select[hideselect]").show();
    },


    FormSubmit: function (formname, btn, posturl, fun) {
        var myForm = $(formname).Validform({
            showAllError: false,
            ajaxPost: true,
            btnSubmit: btn,
            tipSweep: true,
            postonce: false,
            tiptype: function (msg, o, cssctl) {
                if (!o.obj.is("form") && o.type == 3) {
                    $.sp.Alert(msg, false);
                }
            },
            onSubmit: function (formdata) {
                $.sp.SimpleAjax(posturl, $.sp.toJsonString(formdata), function (d) {
                    if ($.isFunction(fun)) {
                        fun(d);
                    }
                });
            }
        });
        return myForm;
    },


    SetHome: function () {
        if (document.all) {
            document.body.style.behavior = 'url(#default#homepage)';
            document.body.setHomePage(window.location);
        } else {
            alert("您好,您的浏览器不支持自动设置页面为首页功能,请您手动在浏览器里设置该页面为首页!");
        }
    },

    AddFavorite: function () {
        sURL = encodeURI(window.location);
        try {

            window.external.addFavorite(window.location, document.title);

        } catch (e) {

            try {

                window.sidebar.addPanel(document.title, window.location, "");

            } catch (e) {

                alert("加入收藏失败，请使用Ctrl+D进行添加,或手动在浏览器里进行设置.");

            }

        }
    },



    SimpleAjax: function (url, data, fun) {
        $.sp.Loading();
        $.ajax({
            url: url,
            type: "POST",
            contentType: "application/json; charset=utf-8",
            async: true,
            dataType: "json",
            data: data,
            success: function (d) {
                $.sp.LoadCompleted();
                if (d.Result == "Error") {
                    $.sp.Alert(d.Message, false);
                } else {
                    if ($.isFunction(fun)) {
                        fun(d.Message);
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.sp.LoadCompleted();
                $.sp.Alert(errorThrown, false);
            }
        });
    },
    toJsonString: function (o) { //主要是推荐这个函数。它将jquery系列化后的值转为name:value的形式。
        var v = '{';
        for (var i in o) {
            v += '"' + o[i].name + '":"' + o[i].value + '",';
        }
        if (v.length > 1) v = v.substring(0, v.length - 1);
        v += '}';
        return v;
    }
};

$(function() {


});